package wang.jinggo.util;

import wang.jinggo.annotation.ISODate;
import wang.jinggo.annotation.ISODateTime;

import java.io.Serializable;
import java.time.Clock;
import java.time.LocalDate;
import java.time.LocalDateTime;

/**
 * @author wangyj
 * @description
 * @create 2017-07-04 19:21
 **/
public class TimePointUtil implements Serializable {

    @ISODate
    private LocalDate day;

    @ISODateTime
    private LocalDateTime date;

    public TimePointUtil(LocalDate day, LocalDateTime date) {
        this.day = day;
        this.date = date;
    }

    public LocalDate getDay() {
        return day;
    }

    public LocalDateTime getDate() {
        return date;
    }

    /**
     * 是否为同一天
     * @param targetDay 目标天数
     * @return
     */
    public boolean equalsDay(LocalDate targetDay) {
        return day.compareTo(targetDay) == 0;
    }

    /**
     * 当天是否在目标天数之前
     * @param targetDay 目标天数
     * @return
     */
    public boolean beforeDay(LocalDate targetDay) {
        return day.compareTo(targetDay) < 0;
    }

    /**
     * 当天是否等于目标天数或者目标天数之前
     * @param targetDay 目标天数
     * @return
     */
    public boolean beforeEqualsDay(LocalDate targetDay) {
        return day.compareTo(targetDay) <= 0;
    }

    /**
     * 当天是否在目标天数之后
     * @param targetDay 目标天数
     * @return
     */
    public boolean afterDay(LocalDate targetDay) {
        return 0 < day.compareTo(targetDay);
    }

    /**
     * 当天是否等于目标天数或者目标天数之后
     * @param targetDay 目标天数
     * @return
     */
    public boolean afterEqualsDay(LocalDate targetDay) {
        return 0 <= day.compareTo(targetDay);
    }

    /**
     * 初始化当天和当天时间
     * @param day
     * @param date
     * @return
     */
    public static TimePointUtil of(LocalDate day, LocalDateTime date) {
        return new TimePointUtil(day, date);
    }

    /**
     * 初始化当天 和 当前的时间
     * @param day
     * @return
     */
    public static TimePointUtil of(LocalDate day) {
        return of(day, day.atStartOfDay());
    }

    /**
     * 当前日期和当前时间
     * @return
     */
    public static TimePointUtil now() {
        LocalDateTime now = LocalDateTime.now();
        return of(now.toLocalDate(), now);
    }

    /**
     * 当前日期和当前时间
     * @param clock
     * @return
     */
    public static TimePointUtil now(Clock clock) {
        LocalDateTime now = LocalDateTime.now(clock);
        return of(now.toLocalDate(), now);
    }
}
